package com.extrace.ui.domain;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.extrace.loader.TransPackageLoader;
import com.extrace.misc.model.TransPackage;
import com.extrace.misc.model.UserInfo;
import com.extrace.net.IDataAdapter;
import com.extrace.ui.main.ExTraceApplication;
import com.zxing.util.CaptureActivity;

public class PackageTransActivity extends AppCompatActivity implements IDataAdapter<TransPackage> {

    private static final int REQUEST_CAPTURE = 100;

    private UserInfo ui;
    private TransPackage item;
    private TransPackageLoader mLoader;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ui = ((ExTraceApplication) getApplication()).getLoginUser();
        if (ui.getName() == null) {
            Toast.makeText(getApplicationContext(), "请登录！", Toast.LENGTH_SHORT).show();
            finish();
        } else if (ui.getURull() != 1) {//登录的用户不是司机，无法使用此功能！
            Toast.makeText(getApplicationContext(), "你无法使用此功能！", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            StartCapture(REQUEST_CAPTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (resultCode) {
            case RESULT_OK:
                switch (requestCode) {
                    case REQUEST_CAPTURE:
                        if (data.hasExtra("BarCode")) {
                            String id = data.getStringExtra("BarCode");
                            try {
                                mLoader = new TransPackageLoader(this,this);
                                mLoader.TransPackageByUID(id);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        finish();
                        break;
                }
        }
    }

    private void StartCapture(int code){
        Intent intent = new Intent();
        intent.putExtra("Action","Captrue");
        intent.setClass(this, CaptureActivity.class);
        startActivityForResult(intent, code);
    }

    @Override
    public TransPackage getData() {
        return item;
    }

    @Override
    public void setData(TransPackage data) {
        item = data;
    }

    @Override
    public void notifyDataSetChanged() {

    }
}
